package com.hsd.util;

import com.hsd.mapper.ILogDao;
import com.hsd.entity.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 操作日志的切面类
 */
@Component
@Aspect
public class LogUtil {
    @Resource
    private ILogDao logDao;

    @AfterReturning(pointcut = "execution(* com.hsd.service.*.*(..))")
    public void doLog(JoinPoint jp) {
        //获取拦截到的切入点的方法名
        String methodName = jp.getSignature().getName();
        //获取切入点方法的参数
        Object[] args = jp.getArgs();
        switch (methodName) {
            case "save"://存钱：记录存钱的日志信息
                double money = Double.parseDouble(args[1].toString());
                String acc = args[0].toString();
                logDao.insertLog(new Log("存钱", money, acc));
                break;
            case "take"://取钱：记录取钱的日志信息
                double money1 = Double.parseDouble(args[1].toString());
                String acc1 = args[0].toString();
                logDao.insertLog(new Log("取钱", money1, acc1));
                break;
            case "transfer"://转账：记录转入转出的日志信息
                //转账金额
                double money2 = Double.parseDouble(args[2].toString());
                //转出
                String acc2_1 = args[0].toString();
                logDao.insertLog(new Log("转出", money2, acc2_1));
                //转入
                String acc2_2 = args[1].toString();
                logDao.insertLog(new Log("转入", money2, acc2_2));
                break;
        }
    }
}
